#!/usr/bin/env python
# encoding: utf-8
import json
import numpy as np
import pandas as pd
from feature_set.base_feature import BaseFeature, RequstData
from utils import preprocess_data
from feature_set.loan.id.loan_id_base_v1 import (LoanIdExtensionV1, LoanIdOnLoanV1, LoanIdOnTimeV1,
                                                 LoanIdOrderV1, LoanIdOutstandingV1, LoanIdOvdV1, LoanIdPreV1)


class LoanIdBaseV1(BaseFeature):
    def __init__(self):
        super().__init__()
        self.function_map = {
            'id_order_v1_feas': self.id_order_v1_feas,
            'id_bill_extension_v1_feas': self.id_bill_extension_v1_feas,
            'id_bill_onloan_v1_feas': self.id_bill_onloan_v1_feas,
            'id_bill_ontime_v1_feas': self.id_bill_ontime_v1_feas,
            'id_bill_outstanding_v1_feas': self.id_bill_outstanding_v1_feas,
            'id_bill_pre_v1_feas': self.id_bill_pre_v1_feas,
            'id_bill_ovd_v1_feas': self.id_bill_ovd_v1_feas,
        }

    def load_conf(self):
        """
        load config file
        Returns:

        """
        pass

    def load_request(self, request_data: RequstData):
        """
        get request data and format feature input data
        Args:
            request_data: input detail data

        Returns:
            format order and installment data
        """
        apply_time = request_data.apply_time
        # order data
        od = request_data.data_sources['order_data_a']
        if not od or od == []:
            order_data = pd.DataFrame(columns=['app_order_id',
                                               'user_id',
                                               'acq_channel',
                                               'device_type',
                                               'product_name',
                                               'product_code',
                                               'product_set_code',
                                               'loan_amount',
                                               'down_payment',
                                               'user_name',
                                               'id_card_number',
                                               'phone_number',
                                               'apply_time',
                                               'product_info',
                                               'status',
                                               'new_old_user_status',
                                               'create_time',
                                               'update_time',
                                               'submit_type'])
        else:
            order_data = pd.DataFrame(od)
        # installment data
        ins_data = request_data.data_sources['installments_data_a']
        if not ins_data or ins_data == []:
            installment_data = pd.DataFrame(columns=['user_id',
                                                     'app_order_id',
                                                     'contract_no',
                                                     'repayment_date',
                                                     'installment_num',
                                                     'installment_amount',
                                                     'principal',
                                                     'interest',
                                                     'cut_interest',
                                                     'service_fee',
                                                     'management_fee',
                                                     'overdue_interest',
                                                     'overdue_days',
                                                     'penalty',
                                                     'extension_fee',
                                                     'repaid_principal',
                                                     'repaid_interest',
                                                     'repaid_cut_interest',
                                                     'repaid_service_fee',
                                                     'repaid_management_fee',
                                                     'repaid_overdue_interest',
                                                     'repaid_penalty',
                                                     'discount_amount',
                                                     'waive_amount',
                                                     'settlement_type',
                                                     'extension_count',
                                                     'status',
                                                     'create_time',
                                                     'update_time'])
        else:
            installment_data = pd.DataFrame(ins_data)
        rst_data = preprocess_data(apply_time, order_data, installment_data)
        self.data = rst_data

    def id_bill_extension_v1_feas(self):
        return LoanIdExtensionV1.get_features(self.data)

    def id_bill_onloan_v1_feas(self):
        return LoanIdOnLoanV1.get_features(self.data)

    def id_bill_ontime_v1_feas(self):
        return LoanIdOnTimeV1.get_features(self.data)

    def id_order_v1_feas(self):
        return LoanIdOrderV1.get_features(self.data)

    def id_bill_outstanding_v1_feas(self):
        return LoanIdOutstandingV1.get_features(self.data)

    def id_bill_ovd_v1_feas(self):
        return LoanIdOvdV1.get_features(self.data)

    def id_bill_pre_v1_feas(self):
        return LoanIdPreV1.get_features(self.data)
